package common

import (
	"context"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/common/form"
	"git.myscrm.cn/golang/stark/v4"
	"strings"
)

type formServer struct {
	formService form.FormServiceIface
}

func NewFormServer(formService form.FormServiceIface) pb.FormServiceServer {
	return &formServer{
		formService: formService,
	}
}

func (f *formServer) GetFormTemplateConfig(ctx context.Context, request *pb.GetFormTemplateConfigRequest) (*pb.GetFormTemplateConfigResponse, error) {
	resp, err := f.formService.GetFormTemplateConfig(ctx, request.TemplateId)

	if err != nil {
		stark.Logger.Error(ctx, "获取模板配置信息出错", err.Error())
		return nil, err
	}
	return resp, nil
}

func (f *formServer) GetFormDataRule(ctx context.Context, request *pb.GetFormDataRuleRequest) (*pb.GetFormDataRuleResponse, error) {
	ruleIds := request.RuleIds
	var uids []string
	if ruleIds != "" {
		uids = strings.Split(",", ruleIds)
	}

	resp, err := f.formService.GetFormDataRuleList(ctx, uids)
	if err != nil {
		stark.Logger.Error(ctx, "获取模板数据校验规则出错", err.Error())
		return nil, err
	}
	return resp, nil
}
